<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入电源编号"
        v-model="queryForm.powerCode"
      >
      </el-input>
      <el-select
        clearable
        style="width: 200px"
        class="filter-item"
        v-model="queryForm.isOnline"
        placeholder="电源在线状态"
      >
        <el-option
          v-for="(val, key) in isOnline"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doSearchQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'power:add'"
        class="filter-item"
        style="margin-left: 10px;"
        @click="openCreateDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="powerCode"
        label="电源编号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="isOnline"
        label="在线状态"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.POWER_ONLINE_STATUS[scope.row.isOnline] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="lockPowerStatus"
        label="锁供电状态"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.POWER_LOCK_STATUS[scope.row.lockPowerStatus] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="devicePowerStatus"
        label="设备供电状态"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.POWER_DEVICE_STATUS[scope.row.devicePowerStatus] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="250"
      >
        <template slot-scope="scope">
          <!-- <el-button size="mini" @click="handleQueryById(scope.row)" title="查看" icon="el-icon-search" circle></el-button> -->
          <el-button
            v-permission="'power:update'"
            size="mini"
            type="primary"
            @click="editDeviceById(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'power:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
          <el-button
            v-permission="'power:lockDialog'"
            size="mini"
            type="primary"
            @click="handlePower(scope.row)"
            title="锁指令"
            icon="el-icon-refresh"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'下发指令'"
      :visible.sync="dialogLockPowerFormVisible"
      width="30%"
    >
      <el-form
        :model="lockPowerForm"
        label-width="120px"
        :rules="lockPowerFormRules"
        ref="lockPowerForm"
      >
        <el-form-item label="电源编号:" prop="powerCode">
          <el-input
            v-model="lockPowerForm.powerCode"
            auto-complete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="供电:" prop="flag" >
          <el-switch v-model="lockPowerForm.flag" active-text="上电" inactive-text="断电" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item> -->
        <el-form-item label="指令:" prop="flag">
          <el-select
            v-model="lockPowerForm.flag"
            placeholder=""
            style="width: 100%;"
          >
            <el-option
              v-for="(val, key) in Dicts.POWER_COMMAND_FLAG"
              :key="key"
              :label="val"
              :value="key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="重复:" prop="type">
          <el-switch
            v-model="lockPowerForm.type"
            active-text="单次"
            inactive-text="每天"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="持续时间:" prop="continues">
          <el-input
            v-model="lockPowerForm.continues"
            auto-complete="off"
            maxlength="5"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogLockPowerFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendPowerCommand">发 送</el-button>
      </div>
    </el-dialog>

    <!-- <el-dialog :title="'设备指令'" :visible.sync="dialogDevicePowerFormVisible" width="30%">
      <el-form :model="devicePowerForm" label-width="120px" :rules="devicePowerFormRules" ref="devicePowerForm" >
         <el-form-item label="电源编号:" prop="powerCode" >
          <el-input v-model="devicePowerForm.powerCode" auto-complete="off" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="供电:" prop="flag" >
          <el-switch v-model="devicePowerForm.flag" active-text="上电" inactive-text="断电" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="重复:" prop="type" >
          <el-switch v-model="devicePowerForm.type" active-text="单次" inactive-text="每天" active-value="1" inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item label="持续时间:" prop="continues" >
          <el-input v-model="devicePowerForm.continues" auto-complete="off"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDevicePowerFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="devicePowerCommand">发 送</el-button>
      </div>
    </el-dialog> -->

    <el-dialog
      :title="`${addForm.rowId ? '编辑' : '新增'}电源`"
      :visible.sync="dialogAddFormVisible"
      width="30%"
    >
      <el-form
        :model="addForm"
        label-width="120px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-form-item label="电源编号:" prop="powerCode">
          <el-input
            v-model="addForm.powerCode"
            auto-complete="off"
            maxlength="20"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAddForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="`${detailForm.rowId ? '编辑' : '新增'}电源`"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <el-form
        :model="detailForm"
        label-width="120px"
        :rules="detailFormRules"
        ref="detailForm"
      >
        <el-form-item label="电源编号:" prop="powerCode">
          <el-input
            readonly="true"
            v-model="detailForm.powerCode"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDetailForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPowerList,
  deletePower,
  queryPowerDetail,
  savePowerDetail,
  addPower,
  powerCommand
} from '@/api/power'

export default {
  name: 'PowerList',
  data() {
    var checkContinues = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('持续时间参数不能为空'))
      }
      if (!/^[0-9]{1,5}$/.test(value) || value > 65535) {
        callback(new Error('请输入1-5位0-65535的数字'))
      } else {
        callback()
      }
    }
    return {
      dialogFormVisible: false,
      dialogAddFormVisible: false,
      dialogLockPowerFormVisible: false,
      dialogDevicePowerFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        importance: undefined,
        title: undefined
      },
      detailFormRules: {
        callWaitTime: [
          { required: true, message: '请输入呼叫等待时间', trigger: 'blur' }
        ]
      },
      lockPowerFormRules: {
        continues: [{ validator: checkContinues, trigger: 'blur' }]
      },
      devicePowerFormRules: {
        continues: [
          { required: true, message: '请输入持续时间参数', trigger: 'blur' }
        ]
      },
      addFormRules: {
        powerCode: [
          { required: true, message: '请输入电源编号', trigger: 'blur' }
        ]
      },
      detailForm: {
        // rowId: 0,
        // videoCallFlag: '',
        // callWaitTime: 0,
        // termiAutoHandupTime: '',
        // resolutionRatio: '',
        // lockOpenFlag: '',
        // alarmTaskTime: '',
        // authCode: '',
        // termiPhoneNum: '',
        // volume: '',
        // callVolume: '',
        // openDoorPwd: '',
        // bluetoothToken: '',
        // morningSoundStart: '',
        // morningSoundEnd: '',
        // afternoonSoundStart: '',
        // afternoonSoundEnd: ''
      },
      addForm: {
        // deviceCode: '',
        // boardAddr: '',
        // ethernetMac: ''
      },
      lockPowerForm: {
        powerCode: '',
        type: '1',
        continues: ''
      },
      devicePowerForm: {
        powerCode: '',
        type: '',
        continues: ''
      },
      isOnline: Dicts.POWER_ONLINE_STATUS,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doSearchQuery() {
      this.queryForm.current = 1
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryPowerList(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该示例, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePower(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    editDeviceById({ rowId }) {
      queryPowerDetail(rowId).then(({ data: { code, data, msg } }) => {
        if (code === 0) {
          this.openEditDialog()
          this.detailForm = {
            rowId: data.rowId,
            powerCode: data.powerCode
          }
        }
      })
    },
    openCreateDialog() {
      this.addForm = {}
      this.dialogAddFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    openEditDialog() {
      this.detailForm = {}
      this.dialogFormVisible = true
      this.$refs.detailForm && this.$refs.detailForm.resetFields()
    },
    openLockDialog() {
      this.lockPowerForm = {}
      this.dialogLockPowerFormVisible = true
      this.$refs.lockPowerForm && this.$refs.lockPowerForm.resetFields()
    },
    openDeviceDialog() {
      this.devicePowerForm = {}
      this.dialogDevicePowerFormVisible = true
      this.$refs.devicePowerForm && this.$refs.devicePowerForm.resetFields()
    },
    saveDetailForm() {
      this.loading = true
      this.$refs.detailForm.validate(valid => {
        if (valid) {
          savePowerDetail(this.detailForm)
            .then(({ data: { data, msg, code } }) => {
              this.doQuery()
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: msg,
                type: 'success'
              })
            })
            .catch(_ => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    saveAddForm() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          addPower(this.addForm)
            .then(({ data: { data, msg, code } }) => {
              this.doQuery()
              this.dialogAddFormVisible = false
              this.$message({
                showClose: true,
                message: msg,
                type: 'success'
              })
            })
            .catch(_ => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    handlePower({ powerCode }) {
      this.openLockDialog()
      this.lockPowerForm = {
        powerCode: powerCode,
        type: '1',
        continues: ''
      }
    },
    sendPowerCommand() {
      this.loading = true
      this.$refs.lockPowerForm.validate(valid => {
        if (valid) {
          powerCommand(this.lockPowerForm)
            .then(({ data: { data, msg, code } }) => {
              this.doQuery()
              this.dialogLockPowerFormVisible = false
              this.$message({
                showClose: true,
                message: msg,
                type: 'success'
              })
            })
            .catch(_ => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    powerCommand() {
      this.loading = true
      this.$refs.devicePowerForm.validate(valid => {
        if (valid) {
          powerCommand(this.devicePowerForm)
            .then(({ data: { data, msg, code } }) => {
              this.doQuery()
              this.dialogDevicePowerFormVisible = false
              this.$message({
                showClose: true,
                message: msg,
                type: 'success'
              })
            })
            .catch(_ => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
